import allure

from config import log
from utils import DriverUtils
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from appium.webdriver.common.mobileby import MobileBy as By


# Page 的基类
class BasePage(object):

    # 在初始化方法中，获取webdriver实例对象
    def __init__(self, type="app"):
        if type == "app":
            self.driver = DriverUtils.get_app_driver()
        elif type == "mis":
            self. driver = DriverUtils.get_mis_driver()

    def find_element(self, values):

        return self.driver.find_element(*values)

    def find_elements(self, values):

        return self.driver.find_elements(*values)

    def base_find_element(self, loc, timeout=20, poll=0.5):
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(lambda x: x.find_element(*loc))

    # 通过不同的方式查找界面元素
    def find_element_if(self, by, value):

        if by == "id":
            element = self.driver.find_element_by_id(value)
            return element
        elif by == "name":
            element = self.driver.find_element_by_name(value)
            return element
        elif by == "xpath":
            element = self.driver.find_element_by_xpath(value)
            return element
        elif by == "classname":
            element = self.driver.find_element_by_class_name(value)
            return element
        elif by == "css":
            element = self.driver.find_element_by_css_selector(value)
            return element
        elif by == "link_text":
            element = self.driver.find_element_by_link_text(value)
            return element
        else:
            print("无对应方法，请检查")
            return None

    # 通过下标获取元素
    def find_elements_if(self, by, value, num):

        if by == "xpath":
            elements = self.driver.find_elements_by_xpath(value)[num]
            return elements
        elif by == "classname":
            elements = self.driver.find_elements_by_class_name(value)[num]
            return elements
        elif by == "css":
            elements = self.driver.find_elements_by_css_selector(value)[num]
            return elements
        elif by == "link_text":
            elements = self.driver.find_elements_by_link_text(value)[num]
            return elements
        else:
            print("无对应方法，请检查")
            return None

    def is_toast_exist(self, text, timeout=20, poll_frequency=0.1):
        """
        判断toast是否存在，是则返回True，否则返回False
        :param text: toast文本
        :param timeout: 定位超时时间
        :param poll_frequency: 查询频率
        :return: True or False
        """

        try:
            toast_loc = (By.XPATH, ".//*[contains(@text, %s)]" % text)
            WebDriverWait(self.driver, timeout, poll_frequency).until(
                ec.presence_of_element_located(toast_loc)
            )
            return True
        except:
            return False

    def get_toast_text(self, toast_loc, timeout=20, poll_frequency=0.1):
        '''
        定位toast元素，获取text属性
        :param toast_loc:
        :param timeout: 元素定位超时时间
        :param poll_frequency: 查询频率
        :return: toast文本内容
        '''

        try:
            toast = WebDriverWait(self, timeout, poll_frequency).until(
                ec.presence_of_element_located(*toast_loc)
            )
            toast_text = toast.get_attribute('text')
            return toast_text
        except Exception as e:
            return e

    def find_move_t(self):
        self.driver.switch_to.context(self.driver.contexts[0])
        self.driver.swipe(800, 1300, 800, 1200, 1600)
        self.driver.switch_to.context(self.driver.contexts[1])

    def find_move_b(self):
        return self.driver.swipe(800, 1200, 800, 1300, 1600)


if __name__ == '__main__':
    BasePage(type="mis")
